package com.kylin.module.community.business;

/*
import java.text.SimpleDateFormat;
import java.text.ParseException;
*/
import java.util.Date;
import java.util.List;

import com.kylin.common.exception.BusinessException;

import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.TransactionDAO;
import com.kylin.module.community.forms.SitemsgForm;
import com.kylin.module.community.orm.Member;
import com.kylin.module.community.orm.MemberDAO;
import com.kylin.module.community.orm.Post;
import com.kylin.module.community.orm.PostDAO;
import com.kylin.module.community.orm.SitemsgDAO;
import com.kylin.module.community.orm.Sitemsg;

import com.kylin.common.paginate.Paging;

import com.kylin.module.community.forms.SitemsgForm;

import com.kylin.module.community.orm.Sitemsg;
import com.kylin.module.community.orm.SitemsgDAO;

import com.kylin.module.user.orm.User;

import com.kylin.module.user.orm.UserDAO;
import com.kylin.common.paginate.Paging;

import com.kylin.module.user.orm.User;

import org.apache.log4j.Logger;


public class SitemsgBlogic {
	
	private int psgeSize = 15;
	protected static Logger log = Logger.getLogger(SitemsgBlogic.class);
 
	/*
	public List getFriends(Long id){
		log.info("get last 10 registers");
		FriendslistDAO fdao = new FriendslistDAO();
		return fdao.findFriends(id);
	}
	*/
	
	/*
	 * 
	 * 
	 */
	public List getFriends(Long userid){
		try{
		User user = (User)BaseHibernateDAO.findById(User.class, userid);
		//User user2 = (User)BaseHibernateDAO.findById(User.class, user2id);
		log.info("get friends list of "+user.getUsername());
		SitemsgDAO fdao = new SitemsgDAO();
		return fdao.find(user);
		}
		catch(BusinessException e){
			return null;
		}
	}

	public List getMsg(User user){
		try{
			SitemsgDAO fdao = new SitemsgDAO();
		return fdao.find(user);
		}
		catch(Exception e){
			return null;
		}
	}	

	public Integer getTotalUnread(Long uid){
		try{
			SitemsgDAO sdao = new SitemsgDAO();
			User user = (User)BaseHibernateDAO.findById(User.class, uid);
			return sdao.findNewById(user);
		}
		catch(Exception e){
			return null;
		}
	}	
	
	/*
	 * 
	 * 
	 */
	public void edit(Long id){
		try{
		User user = (User)BaseHibernateDAO.findById(User.class, id);
		log.info("get last 10 registers"+user.getUsername());
		SitemsgDAO fdao = new SitemsgDAO();
		//return fdao.findFriends(user);
		}
		catch(BusinessException e){
			//return null;
		}
	}	
	
	public Sitemsg findById(Long id){
		SitemsgDAO fdao = new SitemsgDAO();
		return fdao.findById(id);		
	}	
	
	
	public void deleteById(Long id){
		SitemsgDAO fdao = new SitemsgDAO();
		Sitemsg msg =  fdao.findById(id);
		fdao.delete(msg);
	}	
	
	/*
	 * 
	 * 
	 */
	public void save(User user, SitemsgForm sitemsgform){
		
		//log.info("in Sitemsg.save by "+user.getUsername());
		//log.info("in Sitemsg.save, form.toString(): "+sitemsgform.toString());
		try{
						
			Sitemsg model =  new Sitemsg();
			SitemsgDAO dao = new SitemsgDAO();

			model.setFkpsitemsgid(sitemsgform.getFkpsitemsgid());
			model.setMessage(sitemsgform.getMessage());

			/* user in session */
			if (user != null){
				if (user.getUserrole().intValue()==0){
					
					model.setUser(user);
				}
				model.setCreatedby(user.getUsername());
			}
			else{
				log.info("anonymous");
			}
			/*  set User  */

	 			UserDAO  userdao =  new UserDAO();
				User user2 =  userdao.findById(sitemsgform.getUser2id());
				model.setUser2( user2);

			model.setCreatedon(new Date());
			model.setActive(new Short("1"));
			model.setHits(new Integer("0"));
			
			
			dao.save(model);
		}
		catch(BusinessException e){
		
		}
	} 
	
	public Paging show(User user, Integer limitstart, Integer limit){
		Paging page = new Paging();
		SitemsgDAO fdao = new  SitemsgDAO();
		
		page.setTotal(fdao.getTotal(user));
		page.setList(fdao.getList(user, limitstart, limit));
		page.setStart(limitstart);
		page.setLimit(limit);
		
		return page;		
	}	
	
}
